#include<bits/stdc++.h>
using namespace std;
#define gc getchar
#define pc putchar
#define pb push_back
#define mp make_pair
#define ls (id<<1)
#define rs ((id<<1)|1)
#define mid ((l+r)>>1)
#define int long long
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rof(i,a,b) for(int i=(a);i>=(b);--i)

int read(){
    int ret=0,f=0; char ch=gc();
    while(ch<'0'||ch>'9') f|=(ch=='-'),ch=gc();
    while(ch>='0'&&ch<='9') ret=(ret<<1)+(ret<<3)+(ch^48),ch=gc();
    return f?-ret:ret;
}

void write(int x){
    if(!x) return pc('0'),void();
    if(x<0) pc('-'),x=-x;
    int stk[30],tp=0;
    while(x) stk[++tp]=x%10,x/=10;
    while(tp) pc('0'+stk[tp--]);
    return;
}

const int Maxn=2010;
int N,M,V[Maxn],Mx,Mxt;
char T[Maxn],S[Maxn][Maxn],Ans[Maxn];

int Pow(int x,int d,int r=1){while(d){if(d&1) r*=x; x*=x,d>>=1;}return r;}

void calc(){
    int ret=1,cnt=0;
    For(i,1,M){
        int len=strlen(S[i]+1);
        For(l,1,N){
            bool f=1;
            For(j,1,len) if(S[i][j]!=T[l+j-1]){f=0;break;}
            if(f) ret*=V[i],++cnt;
        }
    }
    if(Pow(Mx,cnt)<Pow(ret,Mxt)){
        Mx=ret,Mxt=cnt;
        For(i,1,N) Ans[i]=T[i];
    }
}

void DFS(int x){
    if(x>N) return calc(),void();
    if(T[x]=='.') For(i,0,9) T[x]='0'+i,DFS(x+1),T[x]='.';
    else DFS(x+1);
}

signed main(){
    freopen("magic.in","r",stdin);
    freopen("magic.out","w",stdout);
    N=read(),M=read(); scanf("%s",T+1);
    For(i,1,M) scanf("%s",S[i]+1),V[i]=read();
    DFS(1); For(i,1,N) cout<<Ans[i];
    return 0;
}
